<?php
$loader->requireOnce('includes/inPatientCore/TestNode.class.php');
class TestTree {
	const ROOT_ID = 0;
	private $_root;
	private $_htmlList = '';

	function __construct() {
		$this->_root = new  TestNode('',TestTree::ROOT_ID,'','');
	}
	private function _addNode(&$currNode, &$newNode) {
		if ($currNode->getId() == $newNode->getParentId()) {
			$currNode->addChild($newNode);
			return;

		} else {
			$childrent = $currNode->getArrChildrent();
			foreach ($childrent as &$node) {
				$this->_addNode($node, $newNode);
			}

		}
	}
	public function addNode(&$newNode) {
		$this->_addNode($this->_root, $newNode);
	}
	public function getRoot() {
		return $this->_root;
	}
	/**
	 * build html for test assign desktop.
	 * @return html.
	 */
	public function buildGroupOfTest4DesktopHtmlList($ticketId) {
		
		$this->_buildGroupOfTest4DesktopHtmlList($this->_root, $ticketId);
		//remove empty <ul><ul>
		$this->_htmlList = str_replace("<ul></ul>",'',$this->_htmlList);
		
		return $this->_htmlList;
			
	}
	public function getHtmlList() {
		return $this->_htmlList;
	}
	/*
	public function listOfTestItem($testGroupId) {
		
		$groupNode = $this->findNode($testGroupId);		
		$result = array();
		if ($groupNode != null) {
			$this->_listOfTestItem($groupNode,$result);	
			
		}
		return $result;
	}
	private function _listOfTestItem($currentNode, &$result) {
		
        if ($currentNode->isLeaf() == 1) {
        	
            $id = $currentNode->getId();            
            $result["$id"] = $currentNode->getName();
            return;            
        } 
        
        $arrCurr = $currentNode->getArrChildrent();
        if (!empty($arrCurr)) {
            foreach ($arrCurr as &$node) {
                $this->_listOfTestItem($node, $result);
            }
        } 
    }
    */
	
	public function findNode($testGroupId) {
		return $this->_findNode($this->_root,$testGroupId);
	}
	private function _findNode($currentNode, $testGroupId) {
		
	    if ($currentNode->getId() == $testGroupId) {
	    	return $currentNode;
        }
        		
		$arrCurr = $currentNode->getArrChildrent();
		if (!empty($arrCurr)) {
			foreach ($arrCurr as $node) {
				$result = $this->_findNode($node, $testGroupId);
				if ($result != null) {
					return $result;
				}
			}

		}
		return null;
	}
	
	private function _buildGroupOfTest4DesktopHtmlList($currentNode, $ticketId) {
		
		$arrCurr = $currentNode->getArrChildrent();
		if (empty($arrCurr)) {
			$name = $currentNode->getName();
			$data = $currentNode->getData();
			$action = '';
			if ($data != null) {
			 $action = ' href="javascript:displayTestsOfGroupDesktop(\''. $data['test_id'] . '\',\'' . $data['parent_id'] .'\', \'' . $ticketId . '\')" ';
			}
			$this->_htmlList.= "<li><a " . $action . ">" . $name."</a></li>";


		} else {
			
			if ($currentNode->getId() != TestTree::ROOT_ID) {
				$name = $currentNode->getName();
				$data = $currentNode->getData();
				$action = '';
				if ($data != null) {
					
					$action = ' href="javascript:displayTestsOfGroupDesktop(\''. $data['test_id'] . '\',\'' . $data['parent_id'] .'\', \'' . $ticketId . '\')" ';
				}
				$this->_htmlList .= "<li><a " . $action . ">". $name . "</a><ul>";
			}
			foreach ($arrCurr as &$node) {
				$this->_buildGroupOfTest4DesktopHtmlList($node, $ticketId);
			}
			if ($currentNode->getId() != TestTree::ROOT_ID) {
				$this->_htmlList .= "</ul></li>";
			}

		}

	}

}
?>